<?php

namespace services\marketing;

use Yii;
use common\components\Service;
use common\models\marketing\ClinicAssess;

/**
 * Class ClinicAssessService
 * @package services\marketing
 */
class ClinicAssessService extends Service
{
    /**
     * 根据ID查找信息
     *
     * @param int $id
     * @return ClinicAssess|null
     */
    public function findById(int $id = 0): ?ClinicAssess
    {
        return ClinicAssess::find()->where(['id' => $id])->one();
    }

    /**
     * 查找信息
     *
     * @param array $condition
     * @return ClinicAssess|null
     */
    public function findInfo(array $condition = []): ?ClinicAssess
    {
        $query = ClinicAssess::find();

//        isset($condition['project_id']) && $query->andWhere(['project_id' => $condition['project_id']]);
//        isset($condition['name']) && $query->andWhere(['name' => $condition['name']]);
//        isset($condition['status']) && $query->andWhere(['status' => $condition['status']]);

        return $query->one();
    }

    /**
     * 查找列表
     *
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findList(array $condition = [])
    {
        $query = ClinicAssess::find();

        // select
        isset($condition['select']) && $query->select($condition['select']);

        // condition
        isset($condition['project_id']) && $query->andWhere(['project_id' => $condition['project_id']]);
        isset($condition['status']) && $query->andWhere(['status' => $condition['status']]);

        return $query->asArray()->all();
    }

    /**
     * 诊疗评估列表
     *
     * @return array
     */
    public function getList($projectId)
    {
        $list = ClinicAssess::find()->select('id, assess_name, status, is_inhibit_delete')
            ->where(['status' => [ClinicAssess::STATUS_INVALID, ClinicAssess::STATUS_ENABLE], 'project_id' => $projectId])
            ->orderBy(['status' => SORT_DESC, 'sort' => SORT_ASC])
            ->asArray()
            ->all();

        return [
            'list' => $list,
        ];
    }

    /**
     * 新增诊疗评估
     *
     * @param $params
     * @return array
     */
    public function create($params)
    {
        $exists = $this->checkName([
            'assess_name' => $params['assess_name'],
            'project_id' => $params['project_id'],
            'status' => [ClinicAssess::STATUS_ENABLE, ClinicAssess::STATUS_INVALID]
        ]);
        if ($exists) {
            return ['code' => 422, 'message' => '该诊疗评估名称已存在'];
        }

        $model = new ClinicAssess();
        $model->assess_name = $params['assess_name'];
        $model->project_id = $params['project_id'];
        $model->status = $params['status'];
        $model->is_inhibit_delete = ClinicAssess::INHIBIT_DELETE;
        $model->operation_id = $params['member_id'];
        if ($model->save() == false) {
            return ['code' => 422, 'message' => '新增失败'];
        }
        return ['code' => 200, 'message' => '新增成功'];
    }

    /**
     * 判断是否存在
     *
     * @param $where
     * @return bool
     */
    public function checkName($where)
    {
        return ClinicAssess::find()->where($where)->exists();
    }

    /**
     * 编辑状态
     *
     * @param int $id
     * @param int $status
     * @return array
     */
    public function updateStatus(int $id, int $status)
    {
        $exists = $this->checkName(['id' => $id]);
        if (!$exists) {
            return ['code' => 422, 'message' => '该诊疗评估不存在'];
        }
        $data = ClinicAssess::updateAll(['status' => $status], ['id' => $id]);
        if (!$data) {
            return ['code' => 422, 'message' => '修改失败'];
        }
        return ['code' => 200, 'message' => '修改成功'];
    }

    /**
     * 编辑诊疗评估
     *
     * @param array $params
     * @return array
     */
    public function update(array $params)
    {
        $exists = $this->checkName(['id' => $params['id']]);
        if (!$exists) {
            return ['code' => 422, 'message' => '数据不存在'];
        }

        $model = ClinicAssess::findOne($params['id']);
        $clinicAssessIs = ClinicAssess::findOne(
            [
                'assess_name' => $params['assess_name'],
                'project_id' => $params['project_id'],
                'status' => [ClinicAssess::STATUS_ENABLE, ClinicAssess::STATUS_INVALID],
            ]
        );
        if ($clinicAssessIs && $clinicAssessIs->id != $params['id']) {
            return ['code' => 422, 'message' => '该诊疗评估名称已存在'];
        }

        $model->assess_name = $params['assess_name'];
        $model->project_id = $params['project_id'];
        $model->status = $params['status'];
        $model->is_inhibit_delete = ClinicAssess::INHIBIT_DELETE;
        $model->operation_id = $params['member_id'];
        if ($model->save() == false) {
            return ['code' => 422, 'message' => '修改失败'];
        }
        return ['code' => 200, 'message' => '修改成功'];
    }

    /**
     * 删除诊疗评估
     *
     * @param int $id
     * @return array
     */
    public function delete(int $id)
    {
        $exists = $this->checkName(['id' => $id]);
        if (!$exists) {
            return ['code' => 422, 'message' => '该诊疗评估不存在'];
        }
        $data = ClinicAssess::updateAll(['status' => ClinicAssess::STATUS_DELETE], ['id' => $id]);
        if (!$data) {
            return ['code' => 422, 'message' => '删除失败'];
        }
        return ['code' => 200, 'message' => '删除成功'];
    }
}